package cn.tedu._05mvcboot02.common.exception;

import cn.tedu._05mvcboot02.common.reponse.JsonResult;
import cn.tedu._05mvcboot02.common.reponse.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult toHandlerIllegalArgumentException(IllegalArgumentException ex) {
        log.error("IllegalArgumentException=" + ex.getMessage());
        return new JsonResult(StatusCode.OPERATION_FAILED, ex.getMessage());
    }

    @ExceptionHandler
    public JsonResult toHandlerRuntimeException(RuntimeException ex) {
        log.error("RuntimeException=" + ex.getMessage());
        return new JsonResult(StatusCode.OPERATION_FAILED, ex.getMessage());
    }

    @ExceptionHandler
    public JsonResult toHandlerThrowable(Throwable ex) {
        log.error("Throwable=" + ex.getMessage());
        return new JsonResult(StatusCode.THROWABLE_HANDLER, ex.getMessage());
    }
}
